# 设置最低的 CMake 版本要求
cmake_minimum_required(VERSION 3.10)

# 设置项目名称
project(smartHomeApp)

# 使用 C++17 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 检查是否需要链接 stdc++fs（GCC 8 及以下需要）
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
    message(STATUS "GCC version < 9: linking stdc++fs")
    set(FILESYSTEM_LIB stdc++fs)
else()
    set(FILESYSTEM_LIB "")
endif()

# 设置编译选项
add_compile_options(-Wall -O2 -pthread)

# 手动查找 SQLite3 库和头文件
set(SQLITE3_INCLUDE_DIR /usr/include)
set(SQLITE3_LIBRARIES /usr/lib/x86_64-linux-gnu/libsqlite3.so)
include_directories(${SQLITE3_INCLUDE_DIR})

# 查找 OpenSSL 库，要求找到 Crypto 和 SSL 组件
find_package(OpenSSL REQUIRED COMPONENTS Crypto SSL)

# 包含头文件路径
include_directories(
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/configManager
    ${CMAKE_SOURCE_DIR}/databaseManager
    ${CMAKE_SOURCE_DIR}/deviceManager
    ${CMAKE_SOURCE_DIR}/exceptionManager
    ${CMAKE_SOURCE_DIR}/logManager
    ${CMAKE_SOURCE_DIR}/menuManager
    ${CMAKE_SOURCE_DIR}/multiThreadManager
    ${CMAKE_SOURCE_DIR}/userManager
    ${CMAKE_SOURCE_DIR}/utils/cJSON
    ${CMAKE_SOURCE_DIR}/utils/SHA256
    ${CMAKE_SOURCE_DIR}/utils/ThreadPool
)

# 收集源文件
file(GLOB_RECURSE SRCS
    ${CMAKE_SOURCE_DIR}/main.cpp
    ${CMAKE_SOURCE_DIR}/configManager/*.cpp
    ${CMAKE_SOURCE_DIR}/databaseManager/*.cpp
    ${CMAKE_SOURCE_DIR}/deviceManager/*.cpp
    ${CMAKE_SOURCE_DIR}/exceptionManager/*.cpp
    ${CMAKE_SOURCE_DIR}/logManager/*.cpp
    ${CMAKE_SOURCE_DIR}/menuManager/*.cpp
    ${CMAKE_SOURCE_DIR}/multiThreadManager/*.cpp
    ${CMAKE_SOURCE_DIR}/userManager/*.cpp
    ${CMAKE_SOURCE_DIR}/utils/cJSON/*.c
    ${CMAKE_SOURCE_DIR}/utils/SHA256/*.cpp
    ${CMAKE_SOURCE_DIR}/utils/ThreadPool/*.cpp
)

# 生成可执行文件
add_executable(${PROJECT_NAME} ${SRCS})

# 链接 SQLite3、OpenSSL、std::filesystem（如需要）
target_link_libraries(${PROJECT_NAME}
    ${SQLITE3_LIBRARIES}
    OpenSSL::Crypto
    OpenSSL::SSL
    ${FILESYSTEM_LIB}
)

# 设置可执行文件输出目录
set_target_properties(${PROJECT_NAME} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

# 添加 clean-all 目标（清理构建目录）
add_custom_target(clean-all
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/cmake_clean.cmake
)
